package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/5.
 */

import com.xianzhiOA.bean.XzTempletBase;
import com.xianzhiOA.bean.XzTempletExtend;
import com.xianzhiOA.dao.XzTempletExtendMapper;
import com.xianzhiOA.service.XzTempletExtendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王诗博
 * 扩展模板表
 * @create 2017-05-05 16:09
 **/
@Service("XzTempletExtendService")
public class XzTempletExtendServiceImpl implements XzTempletExtendService {
     @Autowired
    private XzTempletExtendMapper xzTempletExtendMapper;

    //查询模板扩展表中的所有信息
    @Override
    public List<XzTempletExtend> selAllByBaseId(long templetBaseId) {
        List<XzTempletExtend> ExtendList= null;
        ExtendList = xzTempletExtendMapper.selAllByBaseId(templetBaseId);
        return ExtendList;
    }
    //添加扩展模板表
    @Override
    public int insert(XzTempletBase xzTempletBase) {
        int i=0;
        List ExtendTypeList=xzTempletBase.getExtendTypeList();

            //获取已经添加的扩展模板信息的个数
            int oldExtendcount = 0;
            if (xzTempletBase.getExtendObjectList() != null) {
                oldExtendcount = xzTempletBase.getExtendObjectList().size();
            }

            int node = 0;
            for (int j = 0; j < ExtendTypeList.size(); j++) {
                XzTempletExtend newtempletExtend = new XzTempletExtend();
                newtempletExtend.setExtendType(ExtendTypeList.get(j).toString());
                newtempletExtend.setTempletBaseId(xzTempletBase.getTempletBaseId());
                newtempletExtend.setTempletName(xzTempletBase.getTempletName());
                if (oldExtendcount > 0) {
                    if (node == 0) {
                        node = oldExtendcount;
                    }
                    node = node + 1;
                } else {
                    node = j + 1;
                }
                newtempletExtend.setSequenceNum(node);
                System.out.println("newtempletExtend:" + newtempletExtend);
                i = xzTempletExtendMapper.insert(newtempletExtend);
            }

        return i;
    }
    //修改扩展表后的添加方法
    @Override
    public int updateOfinsert(XzTempletBase xzTempletBase) {
        int i=0;
        List<XzTempletExtend> extendObjectList=xzTempletBase.getExtendObjectList();
            for (int j = 0; j < extendObjectList.size(); j++) {
                XzTempletExtend newtempletExtend = extendObjectList.get(j);
                int node = j + 1;
                newtempletExtend.setSequenceNum(node);
                i = xzTempletExtendMapper.insert(newtempletExtend);
            }
        return i;
    }

    //删除扩展模板表
    @Override
    public int deleteExtendByKey(XzTempletExtend xzTempletExtend) {
        int i=0;
         i= xzTempletExtendMapper.deleteExtendByKey(xzTempletExtend);
         return i;
    }

    //修改扩展模板
    @Override
    public int updateExtendByKey(XzTempletExtend xzTempletExtend) {
        int i=0;
        i=xzTempletExtendMapper.updateExtendByKey(xzTempletExtend);
        return i;
    }
    //删除一条扩展模板表中的数据（按id删除）
    @Override
    public int deleteByKey(XzTempletExtend xzTempletExtend) {
        int i=0;
        i=xzTempletExtendMapper.deleteByKey(xzTempletExtend);
        return i;
    }
}
